package com.ym.b2b.dictionary.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.ym.b2b.commons.dao.BaseDao;
import com.ym.b2b.dictionary.dao.EmSDictionaryDAO;
import com.ym.b2b.dictionary.sqlbean.EmSDictionary;
import com.zc.commons.base.Page;

@Repository("emSDictionaryDAO")
public class EmSDictionaryDAOImpl<T> extends BaseDao<T> implements EmSDictionaryDAO {

    @Override
    public void insertEmSDictionary(EmSDictionary record) throws DataAccessException {
        insert("EM_S_DICTIONARY.insertEmSDictionary", record);
    }

    @Override
    public int updateEmSDictionary(EmSDictionary record) throws DataAccessException {
        int rows = sqlMapClient.update("EM_S_DICTIONARY.updateEmSDictionary", record);
        return rows;
    }

    @Override
    public EmSDictionary searchEmSDictionary(Long dictionaryId) throws DataAccessException {
        EmSDictionary key = new EmSDictionary();
        key.setDictionaryId(dictionaryId);
        EmSDictionary record = (EmSDictionary) sqlMapClient.queryForObject("EM_S_DICTIONARY.searchEmSDictionary", key);
        return record;
    }

    @Override
    public int deleteEmSDictionary(Long dictionaryId) throws DataAccessException {
        EmSDictionary key = new EmSDictionary();
        key.setDictionaryId(dictionaryId);
        int rows = sqlMapClient.delete("EM_S_DICTIONARY.deleteEmSDictionary", key);
        return rows;
    }

    @Override
    public Long searchPageEmSDictionaryNum(EmSDictionary emSDictionary) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSDictionary);
        return (Long) listNum("EM_S_DICTIONARY.searchPageEmSDictionaryNum", map);
    }

    @Override
    public Page searchPageEmSDictionary(EmSDictionary emSDictionary, Page<EmSDictionary> page) {
        page.setTotalRow(searchPageEmSDictionaryNum(emSDictionary));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSDictionary);
        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        System.out.println("条数：   " + page.getTotalRow());
        page.setRows(list("EM_S_DICTIONARY.searchPageEmSDictionary", map));
        return page;
    }

    @Override
    public int deleteMultipleEmSDictionary(String dictionaryIdList) {
        int rows = delete("EM_S_DICTIONARY.deleteMultipleEmSDictionary", dictionaryIdList);
        return rows;
    }

    @Override
    public int updateEmSDictionaryBat(EmSDictionary record) {
        int rows = update("EM_S_DICTIONARY.updateEmSDictionaryBat", record);
        return rows;
    }

    @Cacheable(value = "category", key = "#key")
    @Override
    public List<EmSDictionary> showEmSDictionaryByKey(String key) {
        return list("EM_S_DICTIONARY.showEmSDictionaryByKey", key);
    }
    
    @Cacheable(value = "category", key = "#key")
    @Override
    public List<EmSDictionary> showEmSDictionaryByKeyAndType(String key) {
        return list("EM_S_DICTIONARY.showEmSDictionaryByKeyAndType", key);
    }
}
